package com.mnt.gui.base.controller;

import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;

/**
 * main pane out border controller
 * 
 * @author mnt.cico
 * 
 */
public class OutBorderController extends BaseController {

	private Boolean windowIsFullScreen = false;

	@FXML
	private BorderPane bpRoot;

	@FXML
	private Label lblLogo;

	@FXML
	private Label lblTitle;

	@FXML
	private Label lblMax;

	@FXML
	private Label lblClose;

	@FXML
	private Label lblMin;

	/**
	 * configure the label event move, min and max
	 */
	private void configure() {
		lblClose.setOnMouseClicked(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				stage.close();
				Platform.exit();
				System.exit(0);
			}
		});

		lblMin.setOnMouseClicked(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				stage.setIconified(true);// min
			}
		});

		lblMax.setOnMouseClicked(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				fullScreen();
			}
		});
		stage.iconifiedProperty().addListener(new ChangeListener<Boolean>() {
			@Override
			public void changed(ObservableValue<? extends Boolean> observable,
					Boolean oldValue, Boolean newValue) {
				
			}
		});
		lblTitle.setOnMouseClicked(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				if(event.getClickCount() == 2) {
					fullScreen();
				}
			}
		});
		configDragStage(lblTitle);
	}

	/**
	 * configure the title move event
	 * 
	 * @param root
	 */
	private double initX = -1;
	private double initY = -1;

	public void configDragStage(final Node root) {
		// full screen not to drag

		root.setOnMousePressed(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent mouseEvent) {
				if (mouseEvent.isPrimaryButtonDown()) {
					initX = mouseEvent.getScreenX();
					initY = mouseEvent.getScreenY();
					mouseEvent.consume();
				} else {
					initX = -1;
					initY = -1;
				}
			}
		});

		root.setOnMouseReleased(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent mouseEvent) {
				initX = -1;
				initY = -1;
				mouseEvent.consume();
			}
		});

		root.setOnMouseDragged(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent mouseEvent) {
				if (!mouseEvent.isPrimaryButtonDown() || initX == -1) {
					return;
				}

				double newX = mouseEvent.getScreenX();
				double newY = mouseEvent.getScreenY();

				double deltax = newX - initX;
				double deltay = newY - initY;

				initX = newX;
				initY = newY;

				if (stage.isFullScreen())
					return;
				stage.setX(stage.getX() + deltax);
				stage.setY(stage.getY() + deltay);
				mouseEvent.consume();
			}
		});
	}

	/**
	 * full screen opera , if is full screen to min
	 * 2014-3-31 mnt.cico
	 */
	private void fullScreen() {
		windowIsFullScreen = !windowIsFullScreen;
		stage.setFullScreen(windowIsFullScreen);
		if (windowIsFullScreen) {
			lblMax.setId("maximization-conver-icon");
		} else {
			lblMax.setId("maximization-icon");

		}
	}
	
	/**
	 * set context to center
	 * @param root
	 */
	public void setRoot(BaseController root) {
		bpRoot.setCenter(root);
	}
	
	@Override
	public void init() {
		configure();
	}
	
	public final void setTitle(String title) {
		lblTitle.setText(title);
	}

}
